/**
 *
 * 本文件及其相关文件为三味书院网站程序，版权属于北京快乐格子信息技术有限公司，受中国及国际相关法律保护。
 * copyright 2011 www.35shuyuan.com email: law@35shuyuan.com
 *
 * @author nijiayuan
 */
package com.slz3.business.frame.api.tool;

import com.slz3.business.frame.api.alipay.PropertyLoader;

import java.util.Properties;

/**
 * 本类是系统环境变量全局类
 * 
 * @author nijiayuan
 * 
 */
public class AppEnv {
	private static AppEnv instance = null;
	public static String STATIC_PAGE_ROOT_DIR = "static.page.root.dir";//上传文件存储的项目根目录
	public static String FILE_TMP_DIR = "file.tmpDir"; //上传文件临时路径
	public static String STATIC_HTTP_URL = "static.page.http.url";//服务器地址
	public static String STATIC_PAGE_URL = "static.index.page.url";//首页网页地址
	public static String BAIDU_ACCESSKEY="baiduaccessKey";
	public static String BAIDU_SECRETKEY="baidusecretKey";
	public static String BAIDU_BUCKET="baidubucket";
	public static String GAME_TOKEN="game.token";
	// cache server
	public static String CACHE_SERVER_URLS = "cache.server.url";

	private Properties props = null;

	protected AppEnv() {
		try {
			props = PropertyLoader.loadProperties("./application.properties");
		} catch (Exception e) {
		}
		if (props == null) {
			try {
				props = PropertyLoader.loadProperties("application.properties");
			} catch (Exception e) {
			}
		}
	}

	public static AppEnv getInstance() {
		if (instance == null) {
			instance = new AppEnv();
		}

		return instance;
	}

	public Object getProp(final String key) {
		if (props != null) {
			return props.get(key);
		} else {
			return null;
		}
	}

	public void setProp(final String key, final Object value) {
		if (props != null) {
			props.put(key, value);
		}
	}

	public String getStringProp(final String key, final String defaultValue) {
		String value = (String) getProp(key);

		if (value == null) {
			return defaultValue;
		} else {
			return value;
		}
	}

	public String getHttpUrlPath() {
		return AppEnv.getInstance().getStringProp(AppEnv.STATIC_HTTP_URL, "");
	}

	public int getIntProp(final String key, final int defaultValue) {
		String value = (String) getProp(key);
		int intValue = defaultValue;

		if (value != null) {
			try {
				intValue = Integer.parseInt(value);
			} catch (Exception e) {
				intValue = defaultValue;
			}
		}

		return intValue;
	}

	public boolean getBoolProp(final String key, final boolean defaultValue) {
		String value = (String) getProp(key);
		boolean intValue = defaultValue;
		if (value != null) {
			try {
				intValue = Boolean.parseBoolean(value);
			} catch (Exception e) {
				intValue = defaultValue;
			}
		}

		return intValue;
	}

	public static void main(final String args[]) {
		System.out.println(AppEnv.getInstance().getRootPath());
		
	}

	public String getRootPath() {
		return AppEnv.getInstance().getStringProp(AppEnv.STATIC_PAGE_ROOT_DIR,
				"");
	}

}
